package com.xs.ahoi.hcxs.dao;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import com.xs.ahoi.hcxs.Ahoi;
import com.xs.ahoi.hcxs.utils.Constants;
import com.xs.ahoi.hcxs.utils.FileUtil;

/**
 * author: ahoi
 * date: 2018/6/27 11:33
 * description:
 */
public class ConfigDao {

    // 缓存文件名
    public static final String CONFIG_NAME = "hcxs_ahoi_pref";
    private static SharedPreferences sp;
    private static ConfigDao instance;


    // 上次打开的小说ID
    private static final String PK_BOOK_ID = "_BOOK_ID";
    // 字号
    private static final String PK_TEXTSIZE = "_TEXT_SIZE";
    // 行距
    private static final String PK_ROWSPACING = "_ROW_SPACING";
    // 亮度百分比
    private static final String PK_LIGHT = "_LIGHT";
    // 是否系统亮度
    private static final String PK_IS_SYS_LIGHT = "_IS_SYS_LIGHT";
    // 是否常亮
    private static final String PK_IS_ALWAYS_LIGHT = "_IS_ALWAYS_LIGHT";
    // 上次导入目录
    private static final String PK_IMPORT_SCROLL_POSITION = "_IMPORT_POSITION";
    // 背景颜色
    private static final String PK_BG_INDEX = "_BG_INDEX";
    // 编码
    private static final String PK_CHARSET = "_CHARSET";
    // 上次备份时间
    private static final String PK_BACKUPTIME = "_BACKUP_TIME";
    // tts语速
    private static final String PK_TTS_SPEED = "TTS_SPEED";
    // tts音调
    private static final String PK_TTS_TONES = "TTS_TONES";
    // tts语音人类型
    private static final String PK_TTS_SPEAKER = "TTS_SPEAKER";
    // 背景音乐名
    private static final String PK_BGM_NAME = "BGM_NAME";
    // 背景音乐音量
    private static final String PK_BGM_V = "BGM_V";
    // 默认打开的activity
    private static final String PK_CONTENT_ACTIVITY = "CONTENT_ACTIVITY";
    // 是否打开悬浮窗
    private static final String PK_FLOAT_WINS = "FLOAT_WINS";
    // 定时结束时间
    private static final String PK_TIME_OVER = "TIME_OVER";

    private ConfigDao() {
        refresh();
    }

    public static void refresh() {
        sp = Ahoi.getInstance().getApplicationContext().getSharedPreferences(CONFIG_NAME, Context.MODE_MULTI_PROCESS);
    }

    public static synchronized ConfigDao getInstance() {
        if (instance == null) {
            instance = new ConfigDao();
        }
        return instance;
    }

    // 备份缓存文件
    public static void backup() {
        FileUtil.copy(getConfigPath(), Constants.CONFIG_BACKUP_PATH);
    }

    // 恢复缓存文件
    public static void restore() {
        FileUtil.copy(Constants.CONFIG_BACKUP_PATH, getConfigPath());
    }

    // 获取缓存文件路径
    public static String getConfigPath() {
        String basePath;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            basePath = Ahoi.getInstance().getApplicationContext().getDataDir().getAbsolutePath();
        } else {
            basePath = Ahoi.getInstance().getDatabasePath(DBHelper.DATABASE_NAME).getParentFile().getParent();
        }
        return basePath + "/shared_prefs/" + ConfigDao.CONFIG_NAME + ".xml";
    }

    // 获取上次打开的书籍的ID
    public int getBookId() {
        return sp.getInt(PK_BOOK_ID, -1);
    }

    // 设置上次打开的书籍的ID
    public void setBookId(int bookId) {
        sp.edit().putInt(PK_BOOK_ID, bookId).apply();
    }

    // 获取书籍的字号
    public float getTextSize() {
        return sp.getFloat(PK_TEXTSIZE, Constants.DEFAULT_CONTENT_TEXTSIZE);
    }

    // 设置书籍的字号
    public void setTextSize(float size) {
        sp.edit().putFloat(PK_TEXTSIZE, size).apply();
    }

    // 获取亮度
    public int getLight() {
        return sp.getInt(PK_LIGHT, Constants.DEFAULT_LIGHT);
    }

    // 设置亮度
    public void setLight(int light) {
        sp.edit().putInt(PK_LIGHT, light).apply();
    }

    // 获取是否选中系统亮度
    public boolean getIsSystemLight() {
        return sp.getBoolean(PK_IS_SYS_LIGHT, Constants.DEFAULT_IS_SYS_LIGHT);
    }

    // 设置是否选中系统亮度
    public void setIsSystemLight(boolean isSystemLight) {
        sp.edit().putBoolean(PK_IS_SYS_LIGHT, isSystemLight).apply();
    }

    // 获取是否选中屏幕常量
    public boolean getIsAlwaysLight() {
        return sp.getBoolean(PK_IS_ALWAYS_LIGHT, Constants.DEFAULT_IS_ALWAYS_LIGHT);
    }

    // 设置是否选中屏幕常量
    public void setIsAlwaysLight(boolean isAlwaysLight) {
        sp.edit().putBoolean(PK_IS_ALWAYS_LIGHT, isAlwaysLight).apply();
    }


    // 获取文字行距
    public int getRowSpacing() {
        return sp.getInt(PK_ROWSPACING, Constants.DEFAULT_ROW_SPACING);
    }

    // 设置文字行距
    public void setRowSpacing(int rowspacing) {
        sp.edit().putInt(PK_ROWSPACING, rowspacing).apply();
    }

    // 获取上次进入导入页面滚动条位置
    public int getImportScrollPosition(String dirPath) {
        return sp.getInt(dirPath + PK_IMPORT_SCROLL_POSITION, 0);
    }

    // 设置本次进入导入页面滚动条位置
    public void setImportScrollPosition(String dirPath, int position) {
        sp.edit().putInt(dirPath + PK_IMPORT_SCROLL_POSITION, position).apply();
    }

    // 获取上次设置的背景下标
    public int getBackgroundIndex() {
        return sp.getInt(PK_BG_INDEX, 0);
    }

    // 设置上次设置的背景下标
    public void setBackgroundIndex(int index) {
        sp.edit().putInt(PK_BG_INDEX, index).apply();
    }

    // 获取编码
    public String getCharset(String bookPath) {
        return sp.getString(bookPath + PK_CHARSET, FileUtil.getCharset(bookPath));
    }

    // 设置编码
    public void setCharset(String bookPath, String charset) {
        sp.edit().putString(bookPath + PK_CHARSET, charset).apply();
    }

    // 移除编码
    public void removeCharset(String bookPath) {
        sp.edit().remove(bookPath + PK_CHARSET).apply();
    }

    // 获取翻页动画
    public int getPageStyle() {
        return sp.getInt(PK_CHARSET, Constants.PAGE_STYLE_COVER);
    }

    // 设置翻页动画
    public void setPageStyle(int style) {
        sp.edit().putInt(PK_CHARSET, style).apply();
    }

    // 获取上次备份时间
    public long getBackupTime() {
        return sp.getLong(PK_BACKUPTIME, 0);
    }

    // 设置上次备份时间
    public void setBackupTime(long time) {
        sp.edit().putLong(PK_BACKUPTIME, time).apply();
    }

    // 获取tts语速
    public int getTtsSpeed() {
        return sp.getInt(PK_TTS_SPEED, 0);
    }

    // 设置tts语速
    public void setTtsSpeed(int speed) {
        sp.edit().putInt(PK_TTS_SPEED, speed).apply();
    }

    // 获取tts语调
    public int getTtsTones() {
        return sp.getInt(PK_TTS_TONES, 5);
    }

    // 设置tts语调
    public void setTtsTones(int tonesBaidu) {
        sp.edit().putInt(PK_TTS_TONES, tonesBaidu).apply();
    }

    // 获取tts语音人类型
    public int getTtsSpeaker() {
        return sp.getInt(PK_TTS_SPEAKER, 1001);
    }

    // 设置tts语音人类型
    public void setTtsSpeaker(int speaker) {
        sp.edit().putInt(PK_TTS_SPEAKER, speaker).apply();
    }

    // 获取BGM
    public String getBgmName() {
        return sp.getString(PK_BGM_NAME, "");
    }

    // 设置BGM
    public void setBgmName(String name) {
        sp.edit().putString(PK_BGM_NAME, name).apply();
    }

    // 获取BGM音量
    public int getBgmV() {
        return sp.getInt(PK_BGM_V, 80);
    }

    // 设置BGM音量
    public void setBgmV(int v) {
        sp.edit().putInt(PK_BGM_V, v).apply();
    }

    // 获取默认打开的activity
    public boolean getOpenContentActivity() {
        return sp.getBoolean(PK_CONTENT_ACTIVITY, false);
    }

    // 设置默认打开的activity
    public void setOpenContentActivity(boolean flag) {
        sp.edit().putBoolean(PK_CONTENT_ACTIVITY, flag).apply();
    }
    public boolean getFloatWins() {
        return sp.getBoolean(PK_FLOAT_WINS, false);
    }

    // 设置是否打开悬浮窗
    public void setFloatWins(boolean flag) {
        sp.edit().putBoolean(PK_FLOAT_WINS, flag).apply();
    }

    // 获取定时结束时间
    public long getTimeOver() {
        return sp.getLong(PK_TIME_OVER, 0);
    }

    // 设置定时结束时间
    public void setTimeOver(long time) {
        sp.edit().putLong(PK_TIME_OVER, time).apply();
    }
}
